--[[
@classmod Gui
@see Palette
@see CommandBar
]]

local lume = require (LIB_PATH.."lume")
local timer = require "lib.timer"

return pl.class {
_name = "Gui",

_init = function (self, config)
	self.config = config
	assert(self.config, "No configuration given")
	assert(self.config:get_font(), "Unable to get font")
	self.command_bar = require "commandbar"(self.config:get_font())
	self.palette = require "palette"()
	self.tile_display_widget = self.tile_display()
	self.components = {
		self.palette, 
		self.command_bar, 
		self.tile_display_widget}
end,

update = function (self, dt)
	lume.each(self.components,
	function (v)
	if v.update then
	v:update(dt)
	end
	end)
end,

draw = function (self)
	if Sled.state == "map" then
		if Sled.current_map then
			_lg.setColor(1, 1, 1)
			self.position = 0
			self.position = self.position + Sled.config.font:getWidth(" ") + 32
			_lg.print("Map: "..Sled.current_map.name, 30, self.position)
			self.position = self.position + Sled.config.font:getWidth(" ") + 32
			if Sled.current_tile then
				local x, y = Sled.current_tile:get_position()
				_lg.print(string.format("x:%i y:%i", x, y), 30, self.position)
				self.position = self.position + Sled.config.font:getWidth(" ") + 32
			end
			if Sled.current_layer then
				_lg.print("Layer: "..Sled.current_layer.name, 30, self.position)
				self.position = self.position + Sled.config.font:getWidth(" ") + 32
			end
			if Sled.current_marker then
			_lg.print("Marker: "..Sled.current_marker.name, 30, self.position)
				self.position = self.position + Sled.config.font:getWidth(" ") + 32
			end
		end
	end
	lume.each(self.components,
		function (v)
		if v.draw then v:draw() end
	end)
end,

tile_display = pl.class {
	_name = "TileDisplay",
	_init = function (self)
		self.scale = 10
		self.size = 45
		self.x = 30
		self.y = 0
	end,
	
	update = function (self)
		if not self.img then return end
		self.w = self.idata:getWidth() * self.scale
		self.y = Sled.gui.position or 0
		self.img:replacePixels(self.idata)
	end,
	
	redraw = function (self)
		if not Sled.tile_buffer then 
			self.img = nil
			return 
		end
		self.idata = Sled.tile_buffer
		self.img = _lg.newImage(self.idata)
	end,
	
	draw = function (self)
		if Sled.state == "focused" or not self.img then return end
		_lg.setColor(1, 1,1)
		_lg.setLineWidth(.1)
		_lg.rectangle('line', self.x, self.y, self.w, self.w)
		_lg.draw(self.img, self.x, self.y, 0, self.scale, self.scale)
	end,
}

}
